package com.sykj.bsc.zn.controller;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.crypto.hash.SimpleHash;
import org.apache.shiro.util.ByteSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sykj.bsc.rwl.controller.SysUserController;
import com.sykj.bsc.zjj.bean.Bis_shop_post;
import com.sykj.bsc.zn.bean.Bis_beauty_record;
import com.sykj.bsc.zn.bean.Bis_business_order;
import com.sykj.bsc.zn.bean.Bis_cash_record;
import com.sykj.bsc.zn.bean.Bis_shop;
import com.sykj.bsc.zn.bean.Bis_shop_user;
import com.sykj.bsc.zn.service.znServiceImpl;
import com.sykj.bsc.zn.util.ViewExcel;

@Controller
public class BisController {
	@Autowired
	znServiceImpl sus;
	
	/**
	 *商家
	 * @param shop_user
	 * @param model
	 * @return
	 */
	@RequestMapping("finduser_shop")
	public String finduser_shop(Bis_shop_user shop_user,Model model,Bis_shop shop, @RequestParam(value = "page", defaultValue = "1") int page){
		PageHelper.startPage(page,2);
		List<Map> list=sus.finduser_shop(shop_user);
		PageInfo<Map>  info=new  PageInfo<>(list);
		model.addAttribute("info", info);
		
		List<Bis_shop> list3=sus.findshop2();
		model.addAttribute("list3", list3);
		
	
		SimpleDateFormat sdf=new SimpleDateFormat("yyMMddHHmmss");
		String str=sdf.format(new Date());
		Random random=new Random();
		int num= random.nextInt(9999)+1000;
		String shop_user_id=str+num;
		model.addAttribute("shop_user_id", shop_user_id);

		return "backstage/zn/business";

	}
	

	//验证手机号是否重复
	@RequestMapping("checkAccount")
	public void checkAccount(String account,HttpServletResponse resp){
		Map<String,Boolean> map=new HashMap<>();
		boolean bool=sus.checkAccount(account);
		map.put("valid", bool);
		SysUserController.getWirte(resp, map);
	}	
	
	//验证手机号是否重复
		@RequestMapping("checkMobile")
		public @ResponseBody Map<String,Boolean> checkMobile(String mobile){
			Map<String,Boolean> map=new HashMap<>();
			boolean bool=sus.checkMobile(mobile);
			map.put("valid", bool);
			return map;
		}	
	
	@RequestMapping("insertshopuser")
	public String insertshopuser(Bis_shop_user shopuser){
		
		
		SimpleDateFormat sdf1=new SimpleDateFormat("yyMMddHHmmss");
		String str1=sdf1.format(new Date());
		Random random1=new Random();
		int num1= random1.nextInt(9999)+1000;
		String shop_user_id=str1+num1;
		String hashAlgorithName="MD5";
		String pass="123";
		//盐值
		Object salt=ByteSource.Util.bytes(shopuser.getAccount());
		//加密次数
		int count=1024;
		//进行加密
		Object result=new SimpleHash(hashAlgorithName, pass,salt,count);
		shopuser.setPassword(result.toString());
		shopuser.setShop_user_id(shop_user_id);
		sus.insertshopuser(shopuser);
		return "redirect:/finduser_shop";
	}
	
	@RequestMapping("deleteshopuser1/{shop_user_id}")
	public String deleteshop1(@PathVariable String shop_user_id){
		sus.deleteshopuser1(shop_user_id);
		return "redirect:/finduser_shop";
	}
	
	@RequestMapping("toupdateshopuser/{shop_user_id}")
	public String toupdateshopuser(@PathVariable  String shop_user_id,Model model,Bis_shop shop){
		Bis_shop_user updshopuser=sus.toupdateshop(shop_user_id);
		model.addAttribute("updshop", updshopuser);
		
		List<Map> list3=sus.findshop(shop);
		model.addAttribute("list3", list3);
		
		return "backstage/zn/updateshopuser";
	}
	
	@RequestMapping("updateshopuser")
	public String updateshopuser1(Bis_shop_user shopuser){
		sus.updateshopuser(shopuser);
		return "redirect:/finduser_shop";
	}
	
	/*//验证原密码
	@RequestMapping("judgepass")
	@ResponseBody
	public String judgepass(Bis_shop_user shopuser,String oldpass,String account){
		String hashAlgorithName="MD5";
		String pass=oldpass;
		System.out.println(pass+"未加密原密码");
		System.out.println(account+"账号");
		//盐值
		Object salt=ByteSource.Util.bytes(shopuser.getAccount());
		//加密次数
		int count=1024;
		//进行加密
		Object result=new SimpleHash(hashAlgorithName, pass,salt,count);
		System.out.println(result+"加密后原密码");
		 shopuser=sus.toupdateshop(shopuser.getShop_user_id());
		 if(shopuser.getPassword().equals(result.toString())){
			 return "{\"res\":\"1\"}";	
		 }else {
			 return "{\"res\":\"0\"}";
		}
		
	}*/
	
	
	//修改密码
	/*@RequestMapping("updatepassword")
	public String updatepassword(Bis_shop_user shopuser){
		String hashAlgorithName="MD5";
		String pass=shopuser.getPassword();
		//盐值
		Object salt=ByteSource.Util.bytes(shopuser.getAccount());
		//加密次数
		int count=1024;
		//进行加密
		Object result=new SimpleHash(hashAlgorithName, pass,salt,count);
		shopuser.setPassword(result.toString());
		sus.updatepassword(shopuser);
		return "redirect:/finduser_shop";
	}*/
	
	@RequestMapping("findshop1")
	public String findshop1(Model model,Bis_shop shop, @RequestParam(value = "page", defaultValue = "1") int page,String user_type){
		PageHelper.startPage(page,2);
		List<Map> list=sus.findshop1(shop);
		PageInfo<Map>  info=new  PageInfo<>(list);
		model.addAttribute("info", info);
		
		List<Map> list2=sus.findshopuserBytype(user_type);
		model.addAttribute("list2", list2);
		return "backstage/zn/shopkeeper";
	}
	
	@RequestMapping("insertshop")
	public String insershop(Bis_shop shop,@RequestParam("file")MultipartFile[] file,HttpServletRequest request){
		String path=request.getRealPath("/");//tomcat/project
		File file1=new File(path);
		//path=file1.getParent();//webapp
		path=path+"/upload/";
		Date date=new Date();
		SimpleDateFormat sdf=new SimpleDateFormat("yyyyMMdd");
		//path:c:/tools/tomcat/webapp/upload/20180719
		path=path+sdf.format(date);
		File dir=new File(path);
		if(!dir.exists()){
			//如果该目录不存在则创建目录
			dir.mkdirs();
		}
		File shop_image=new File(path,file[0].getOriginalFilename());
		File shop_detail_image=new File(path,file[1].getOriginalFilename());
		try{
			if(!shop_image.exists()){
				//此时创建之后只是一个空的文件
				shop_image.createNewFile();
			}
			//将file中的内容复制到newfile中
			file[0].transferTo(shop_image);
			file[1].transferTo(shop_detail_image);
		}catch(Exception e){
			e.printStackTrace();
		}
		
		shop.setShop_image("/upload/"+sdf.format(date)+"/"+file[0].getOriginalFilename());
		shop.setShop_detail_image("/upload/"+sdf.format(date)+"/"+file[1].getOriginalFilename());
		sus.insertshop(shop);
		return "redirect:/findshop1";
	}
	
	@RequestMapping("deleteshop/{shop_id}")
	public String deleteshopuser(@PathVariable int shop_id,Model model){
		sus.deleteshop(shop_id);
		return "redirect:/findshop1";
	}
	
	@RequestMapping("toupdateshop2/{shop_id}")
	public String toupdateshop2(@PathVariable int shop_id,Bis_shop shop,Model model){
		shop=sus.toupdateshop2(shop_id);
		model.addAttribute("list", shop);
		return "backstage/zn/updateshop";
	}
	
	@RequestMapping("updateshop")
	public String updateshop(Bis_shop shop,@RequestParam("file")MultipartFile[] file,HttpServletRequest request){
		String path=request.getRealPath("/");//tomcat/project
		File file1=new File(path);
		//path=file1.getParent();//webapp
		path=path+"/upload/";
		Date date=new Date();
		SimpleDateFormat sdf=new SimpleDateFormat("yyyyMMdd");
		//path:c:/tools/tomcat/webapp/upload/20180719
		path=path+sdf.format(date);
		File dir=new File(path);
		if(!dir.exists()){
			//如果该目录不存在则创建目录
			dir.mkdirs();
		}
		File shop_image=new File(path,file[0].getOriginalFilename());
		File shop_detail_image=new File(path,file[1].getOriginalFilename());
		try{
			if(!shop_image.exists()){
				//此时创建之后只是一个空的文件
				shop_image.createNewFile();
			}
			//将file中的内容复制到newfile中
			file[0].transferTo(shop_image);
			file[1].transferTo(shop_detail_image);
		}catch(Exception e){
			e.printStackTrace();
		}
		String a=file[0].getOriginalFilename();
		if(a!=null &&!a.equals("") ){
			shop.setShop_image("/upload/"+sdf.format(date)+"/"+file[0].getOriginalFilename());
		}
		String b=file[1].getOriginalFilename();
		if(b!=null &&!b.equals("") ){
			shop.setShop_detail_image("/upload/"+sdf.format(date)+"/"+file[1].getOriginalFilename());
		}
		sus.updateshop(shop);
		return "redirect:/findshop1";
	}
	/**
	 * 财务
	 */
	//现金流水
	@RequestMapping("findcash")
	public String findcash(Model model,Bis_cash_record cash){

		List<Map> list=sus.findcashrecord(cash);
		model.addAttribute("list", list);
		return "backstage/zn/cash";
	}
	
	@RequestMapping("deletecash/{record_id}")
	public String deletecash(@PathVariable String record_id,Model model){
		sus.deletecash(record_id);
		return "redirect:/findcash";
	}
	
	//颜值流水
	@RequestMapping("findbeautyrecord")
	public String findbeautyrecord(Model model,Bis_beauty_record beautyrecord){
		List<Map> list=sus.findbeautyrecord(beautyrecord);
		model.addAttribute("list", list);
		return "backstage/zn/beautyrecord";
	}
	
	//财务总控
	@RequestMapping("findshop")
	public String findshop(Model model,Bis_shop shop){
		//table
		List<Map> list=sus.findshop(shop);
		model.addAttribute("list", list);
		
		List<Map>list2=sus.findshop1(shop);
		model.addAttribute("list2", list2);
		
		Bis_business_order businessorder=sus.findbusinessorder();
		model.addAttribute("businessorder", businessorder);
		return "backstage/zn/control";
	}

	
	
	
	//导出excel
	@RequestMapping(value="cashexcel",produces = "text/plain;charset=utf-8")
	 public ModelAndView export(ModelMap map,Bis_cash_record cash )throws Exception{
		Map<String, Object> conditionMap = new HashMap<String, Object>();
		
		List<Map<String, String>> list= sus.cashexcel(cash);

       String[] titles={"流水号","订单编号","顾客账号","姓名","颜值数量","支付方式","实际消费金额","支付时间"};
       ViewExcel excel=new ViewExcel(titles);
       map.put("excelList", list);
       return new ModelAndView(excel,map);
   }
	
	@RequestMapping(value="beautryrecordexcel",produces = "text/plain;charset=utf-8")
	 public ModelAndView beautryrecord(ModelMap map,Bis_beauty_record beaurtyrecord )throws Exception{
		Map<String, Object> conditionMap = new HashMap<String, Object>();
		List<Map<String, String>> list= sus.beautryrecordexcel(beaurtyrecord);
      String[] titles={"流水号","订单编号","顾客账号","姓名","颜值数量","实际消费金额","分类","支付时间"};
      ViewExcel excel=new ViewExcel(titles);
      map.put("excelList", list);
      return new ModelAndView(excel,map);
  }
	
	@RequestMapping(value="shopexcel",produces = "text/plain;charset=utf-8")
	 public ModelAndView shop(ModelMap map,Bis_shop shop )throws Exception{
		Map<String, Object> conditionMap = new HashMap<String, Object>();
		List<Map<String, String>> list= sus.shopexcel(shop);
     String[] titles={"店铺编号","店铺名称","店铺简称","店铺现金收入","现金收入目标业绩","完成率","店铺消耗收入","消耗收入目标业绩","完成率","免费颜值收入"};
     ViewExcel excel=new ViewExcel(titles);
     map.put("excelList", list);
     return new ModelAndView(excel,map);
 }
	
	//填充职务下拉框
	@RequestMapping("selectBis_shop_post")
	public @ResponseBody List<Bis_shop_post> selectBis_shop_post(){
		return sus.selectBis_shop_post();
	}
}